{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def convert_nb_to_script(dir_):\n",
    "    ! jupyter nbconvert --to script $dir_/*.ipynb\n",
    "\n",
    "    #.txt means file was open/not saved so converted to .txt not .py\n",
    "    for fil in os.listdir(dir_):\n",
    "        assert '.txt' not in fil, \"Save all your notebooks first!\"\n",
    "    if dir_ is not \".\":\n",
    "        ! mv $dir_/__init__.py $dir_/__init__.pyc\n",
    "        ! mv $dir_/nbfinder.py $dir_/nbfinder.pyc        \n",
    "\n",
    "\n",
    "    ! sed -i.bak 's/# coding: utf-8/import matplotlib; matplotlib.use(\"agg\")/g' $dir_/*.py\n",
    "\n",
    "    #get rid of IN[] lines\n",
    "    !sed -i.bak '/# IN\\[*/d' $dir_/*.py\n",
    "\n",
    "    #get rid of ipython lines\n",
    "    !sed -i.bak '/ipython*/d' $dir_/*.py\n",
    "\n",
    "    !sed -i.bak '/NotebookFinder*/d' $dir_/*.py\n",
    "\n",
    "    ! sed -i.bak 's/notebooks./scripts./g' $dir_/*.py\n",
    "    #get rid of IN[] lines\n",
    "    !sed -i.bak '/# In\\[*/d' $dir_/*.py\n",
    "\n",
    "    !sed -i.bak '/# coding:*/d' $dir_/*.py\n",
    "\n",
    "    !sed -i.bak 's/plt.show()*/pass/g' $dir_/*.py\n",
    "\n",
    "    ! rm $dir_/*.bak\n",
    "    \n",
    "\n",
    "    if dir_ is not \".\":\n",
    "        subdir = \"/\".join(dir_.split(\"notebooks/\")[1:])\n",
    "        if not os.path.exists(os.path.join(\"./scripts\", subdir)):\n",
    "            os.makedirs(os.path.join(\"./scripts\", subdir))\n",
    "        #move to scripts\n",
    "        ! mv $dir_/*.py ./scripts/$subdir\n",
    "        ! mv $dir_/__init__.pyc $dir_/__init__.py\n",
    "        ! mv $dir_/nbfinder.pyc $dir_/nbfinder.py\n",
    "    else:\n",
    "        ! rm $dir_/convert_nb_to_scripts.py\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./notebooks\n",
      "[NbConvertApp] Converting notebook ./notebooks/build_network.ipynb to script\n",
      "[NbConvertApp] Writing 13241 bytes to ./notebooks/build_network.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/configs.ipynb to script\n",
      "[NbConvertApp] Writing 3820 bytes to ./notebooks/configs.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/helper_fxns.ipynb to script\n",
      "[NbConvertApp] Writing 12660 bytes to ./notebooks/helper_fxns.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/print_n_plot.ipynb to script\n",
      "[NbConvertApp] Writing 8822 bytes to ./notebooks/print_n_plot.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/run_dir.ipynb to script\n",
      "[NbConvertApp] Writing 1382 bytes to ./notebooks/run_dir.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/train_val.ipynb to script\n",
      "[NbConvertApp] Writing 8338 bytes to ./notebooks/train_val.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/tsne_minimal.ipynb to script\n",
      "[NbConvertApp] Writing 1142 bytes to ./notebooks/tsne_minimal.py\n",
      "./notebooks/load_data\n",
      "[NbConvertApp] Converting notebook ./notebooks/load_data/event_counter.ipynb to script\n",
      "[NbConvertApp] Writing 3212 bytes to ./notebooks/load_data/event_counter.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/load_data/ground_truth_maker.ipynb to script\n",
      "[NbConvertApp] Writing 4753 bytes to ./notebooks/load_data/ground_truth_maker.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/load_data/label_loader.ipynb to script\n",
      "[NbConvertApp] Writing 2445 bytes to ./notebooks/load_data/label_loader.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/load_data/netcdf_loader.ipynb to script\n",
      "[NbConvertApp] Writing 5742 bytes to ./notebooks/load_data/netcdf_loader.py\n",
      "[NbConvertApp] Converting notebook ./notebooks/load_data/util.ipynb to script\n",
      "[NbConvertApp] Writing 4314 bytes to ./notebooks/load_data/util.py\n",
      "[NbConvertApp] Converting notebook ./convert_nb_to_scripts.ipynb to script\n",
      "[NbConvertApp] Writing 2002 bytes to ./convert_nb_to_scripts.py\n",
      "[NbConvertApp] Converting notebook ./hur_main.ipynb to script\n",
      "[NbConvertApp] Writing 526 bytes to ./hur_main.py\n"
     ]
    }
   ],
   "source": [
    "for dirpath, dirs, files in os.walk(\"./notebooks\"):\n",
    "    if not \".ipynb_checkpoint\" in dirpath:\n",
    "        print dirpath\n",
    "        convert_nb_to_script(dirpath)\n",
    "convert_nb_to_script(\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
